home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* antialias.c - shows how to enable antialiasing for points and
- * lines
- *
- * Escape key - exit the program
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mousebutton - change the twist angle based on
- * horizontal mouse movement
- * Right Mousebutton - zoom in and out based on vertical
- * mouse movement
- * <a> Key - toggle antialiasing on / off
- * <b> key - toggle blend function
- * <o> Key - change the drawing order
- * <R> key - reset view
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
- GLvoid initialize( char * );
- GLvoid blendFuncCycle( GLvoid );
- GLvoid antialiasing( GLvoid );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLboolean antialiasFlag = GL_FALSE;
- static GLboolean drawBackToFront = GL_TRUE;
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLint xStart = 0, yStart = 0;
-
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates point and line antialiasing\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "<a> Key - toggle antialiasing on / off\n"
- "<b> key - toggle blend function\n"
- "<o> Key - change the drawing order\n"
- "<R> Key - reset view\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxObjectSize = 3.0;
-
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
- glShadeModel( GL_FLAT );
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxObjectSize;
-
- resetView();
-
- /* set the initial blend function */
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
-
- /* make the point size large so that the points
- * don't disappear when antialiasing is enabled */
- glPointSize( 5.5 );
- glLineWidth( 4.5 );
-
- glEnable( GL_DEPTH_TEST );
- printf("depth buffer is enabled\n");
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- antialiasing( GLvoid )
- {
- antialiasFlag = !antialiasFlag;
-
- /* Toggle anti-aliasing for each type of primitive */
- if ( antialiasFlag == GL_TRUE ) {
- glEnable( GL_POINT_SMOOTH );
- glEnable( GL_LINE_SMOOTH );
- } else {
- glDisable( GL_POINT_SMOOTH );
- glDisable( GL_LINE_SMOOTH );
- }
- printf("antialiasing %s\n", (antialiasFlag? "ON" : "OFF"));
- }
-
- GLvoid
- blendFuncCycle( GLvoid )
- {
- static int whichBlendFunc = 0;
-
- whichBlendFunc = (whichBlendFunc + 1) % 2;
-
- switch (whichBlendFunc)
- {
- case 0:
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
- break;
- case 1:
- glBlendFunc( GL_SRC_ALPHA, GL_ONE );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE )\n" );
- break;
- default:
- break;
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'a': /* toggle antialiasing */
- antialiasing();
- glutPostRedisplay();
- break;
- case 'o': /* change drawing order */
- drawBackToFront = !drawBackToFront;
- printf("drawing order: %s\n",
- (drawBackToFront? "Back to Front" : "Front to Back"));
- glutPostRedisplay();
- break;
- case 'b': /* cycle through several blend funcs */
- blendFuncCycle();
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- static GLint buttons_down = 0;
-
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- if (--buttons_down == 0)
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- void
- drawBackRectangle( GLvoid )
- {
- static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
-
- glColor4fv( magenta );
- glutSolidCube( 2.0 );
- }
-
-
- void
- drawAntialiasedObjects()
- {
- static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
-
- if ( antialiasFlag == GL_TRUE ) {
- /* enable blending */
- glEnable( GL_BLEND );
- }
-
- /* draw a point in front of the cube */
- glPushMatrix();
- glTranslatef( 0.0, 0.0, 1.1 );
-
- /* Draw a point */
- glColor4fv( yellow );
- glBegin( GL_POINTS );
- glVertex2f( 0.0, 0.5 );
- glEnd();
- glPopMatrix();
-
- /* Draw a wireframe icosahedron surrounding the cube */
- glPushMatrix();
- glColor4fv( yellow );
- glScalef( 2.0, 2.0, 2.0 );
- glutWireIcosahedron();
- glPopMatrix();
-
- if ( antialiasFlag == GL_TRUE ) {
- /* disable blending */
- glDisable( GL_BLEND );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- glRotatef( 15.0, 1.0, 1.0, 0.0 );
-
- if ( drawBackToFront ) {
- drawBackRectangle();
- drawAntialiasedObjects();
- } else {
- drawAntialiasedObjects();
- drawBackRectangle();
- }
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-